﻿using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using zhangmenren.IService;
using zhangmenren.Models.Enums;
using zhangmenren.Models.help;
using zhangmenren.Untils;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace zhangmenren.Controllers
{
	public class JiaoChengZLController : Controller
	{
		private IWorksService worksService;
		private ICacheService cacheService;
		private IBannerService bannerService;
        private ICensusService censusService;
        private IChapterService chapterService;
		public JiaoChengZLController(IChapterService _chapterService, ICensusService _censusService, IWorksService _worksService, ICacheService _cacheService, IBannerService _bannerService)
		{
			worksService = _worksService;
			cacheService = _cacheService;
			bannerService = _bannerService;
            censusService = _censusService;
            chapterService = _chapterService;
        }
		// GET: /<controller>/
		public async Task<IActionResult> Index()
		{

			ViewBag.Banner = await GetData.GetBanner(bannerService, cacheService, "JiaoChengZL", BannerType.JCZL);
			var result = await worksService.Selelct(1, 50, "");
			var list = result.ToList().FindAll(p => p.Type == WorkType.WENDANG);
			return View(list);
		}


        [HttpPost]
        public async Task< JsonResult> Search(string key)
        {
           var result=  await worksService.Selelct(1, 50, key);
            var list = result.ToList().FindAll(p => p.Type == WorkType.WENDANG);
            return Json(new { data = list, state = "ok" ,msg="查询成功"});
        }

        public async Task<IActionResult> Detail(int id)
		{

            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            ViewBag.ISOK = false;
            var result = await worksService.FindById(id);
            if (user == null)
            {
                return View(result);
            }
            var census = await censusService.FindByUserId(user.Id);
            var num = census.ToList().FindAll(p => p.Type == 3 && p.WorkId == id)?.Count ?? 0;
            if (num > 0)
            {
                ViewBag.ISOK = true;
                ViewBag.List= await chapterService.FindByWork(id);
            }
            return View(result);
        }

	}
}
